    //
//  BSMapViewController.m
//  BSValue2
//
//  Created by Rainbow on 1/4/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "MapViewController.h"

@implementation MapViewController
@synthesize _annotations;

-(id)initWithAnnotation:(NSArray *)annotations
{
	if ((self = [super init])) {
		self._annotations = annotations;
	}
	return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *logoView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]] autorelease];
    logoView.frame = CGRectMake(0, 0, 94, 29);
    self.navigationItem.titleView = logoView;
    
    
    UIButton *backBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(tapBackBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    self.navigationItem.leftBarButtonItem = backItem;
    
    CGRect bounds = [[UIScreen mainScreen] bounds];
	mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height-self.navigationController.navigationBar.bounds.size.height-[UIApplication sharedApplication].statusBarFrame.size.height)];
	mapView.delegate = self;
	MKCoordinateRegion region;
    ITSAnnotation *annotation = [self._annotations objectAtIndex:0];
	region.center.latitude = annotation.coordinate.latitude;
	region.center.longitude = annotation.coordinate.longitude;
	region.span.latitudeDelta = 0.01;
	region.span.longitudeDelta = 0.01;
	[mapView setRegion:region];
	[self.view addSubview:mapView];
	
	[mapView addAnnotations:self._annotations];
	
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    [mapView release];
    mapView = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setToolbarHidden:YES animated:NO];
}


-(void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	[titleLabel removeFromSuperview];
}

#pragma mark MKMapViewDelegate Methods
- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation{
	NSString* identifier = @"identifier";
	
	MKPinAnnotationView *view = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
	
	if(nil == view)
	{
		view = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
	}
	view.animatesDrop = YES;
	[view setEnabled:YES];
	[view setCanShowCallout:YES];
	return view;
}

-(void)tapBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
    [mapView release];
	[titleLabel release];
    [_annotations release];
    [super dealloc];
}

@end

@implementation ITSAnnotation

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

-initWithCoordinate:(CLLocationCoordinate2D)inCoord
{
	if ((self = [super init])) {
		self.coordinate = inCoord;
	}
	return self;
}

-(void)dealloc
{
	[title release];
	[subtitle release];
	[super dealloc];
}

@end
